package com.itheima.controller;

import com.itheima.aspect.Log;
import com.itheima.aspect.MyAnno;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import com.itheima.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/*员工 */

@Slf4j
@RestController
@RequestMapping("/emps")

//@Scope("prototype") bean的作用域, 默认是单例的
public class EmpController {
    @Lazy
    @Autowired
    private EmpService empService;

    public EmpController(){
        log.info("empController-----------------------------------");
    }

    /**
     * 分页查询员工
     *
     * @param empQueryParam
     * @return
     */
    @MyAnno(name = "kenken")
    @GetMapping
    public Result pageEmp(EmpQueryParam empQueryParam) {
        PageBean pageBean = empService.pageEmp(empQueryParam);
        return Result.success(pageBean);
    }

    /**
     * 新增员工
     *
     * @param emp
     * @return
     */
    @Log
    @PostMapping
    public Result insertEmp(@RequestBody Emp emp) {
        empService.insertEmp(emp);
        return Result.success();
    }

    /**
     * 批量删除员工信息
     *
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping
    public Result removeEmps(@RequestParam List<Integer> ids) {
        empService.removeEmps(ids);
        return Result.success();
    }

    /**
     * 根据id查询员工及工作经历
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Emp emp = empService.selectById(id);
        return Result.success(emp);
    }

    /**
     * 修改员工信息
     *
     * @param emp
     * @return
     */
    @Log
    @PutMapping
    public Result updateById(@RequestBody Emp emp) {
        empService.updateById(emp);
        return Result.success();
    }

    /**
     * 查询所有员工
     *
     * @return
     */
    @GetMapping("/list")
    public Result getEmpList() {
        List<Emp> empList = empService.getEmpList();
        return Result.success(empList);
    }
}
